<script setup>
import * as echarts from 'echarts'
import { onMounted, ref, watch } from 'vue'

const props = defineProps({
  msg: String,
  data: {
    type: Object,
    required: true
  }
})
console.log('props=>data', props.data)
// 1.初始化echarts实例
let myChart = null
const targetV = ref(null)

onMounted(() => {
  myChart = echarts.init(targetV.value)
  renderChart()
})

const getSeriesData = () => {
  const series = []
  // 上层
  props.data.forEach((item, index) => {
    series.push({
      name: item.name,
      type: 'pie',
      clockWise: false,
      hoverAnimation: false,
      radius: [ 73 - index * 15 + '%', 68 - index * 15 + '%' ],
      center: ['55%', '55%'],
      label: {
        show: false
      },
      data: [
        { value: item.value, name: item.name },
        { value: 1000, itemStyle: { color: 'rgba(0,0,0,0)', borderWidth: 0} }
      ],
      tooltip: { show: true },
      hoverAnimation: false
    })
    // 底层
    series.push({
      name: item.name,
      type: 'pie',
      silent: true,
      z: 1,
      clockWise: false,
      hoverAnimation: false,
      radius: [ 73 - index * 15 + '%', 68 - index * 15 + '%' ],
      center: ['55%', '55%'],
      label: {
        show: false
      },
      data: [
        { 
          value: 7.5,
          itemStyle: { color: 'rgb(3,31,62)', borderWidth: 0 }, 
          tooltip: {
            show: false,
          },
          hoverAnimation: false
        },
        { 
          value: 2.5,
          itemStyle: { color: 'rgba(0,0,0,0)', borderWidth: 0 }, 
          tooltip: {
            show: false
          },
          hoverAnimation: false
        }
      ],
      tooltip: { show: true },
      hoverAnimation: false
    })
  })
  return series
}

// 2.构建option配置对象
const renderChart = () => {
  const options = {
    // 图例配置
    legend: {
      show: true,
      icon: 'circle',
      top: '16%',
      left: '58%',
      data: props.data.map(item => item.name),
      width: -5,
      itemHeight: 20,
      itemWidth: 10,
      itemGap: 9,
      textStyle: {
        fontSize: 12,
        lineHeight: 10,
        color: '#fff'
      }
    },
    // 提示层
    tooltip: {
      show: true,
      trigger: 'item',
      formatter: '{a}({d}%)'
    },
    // Y 轴
    yAxis: [{
      type: 'category',
      inverse: true,
      axisLine: {
        show: false
      }
    }],
    // X 轴
    xAxis: [
      {
        show: true
      }
    ],
    // 核心配置
    series: getSeriesData()
    // tooltip: {
    //   trigger: 'item',
    //   formatter: '{a} <br/>{b} : {c} ({d}%)'
    // },
    // legend: {
    //   left: 'center',
    //   top: 'bottom',
    //   data: [
    //     'rose1',
    //     'rose2',
    //     'rose3',
    //     'rose4',
    //     'rose5',
    //     'rose6',
    //     'rose7',
    //     'rose8'
    //   ]
    // },
    // toolbox: {
    //   show: true,
    //   feature: {
    //     color: '#fff',
    //     mark: { show: true },
    //     dataView: { show: true, readOnly: false },
    //     restore: { show: true },
    //     saveAsImage: { show: true }
    //   }
    // },
    // series: [
    //   {
    //     type: 'pie',
    //     radius: [70, 100],
    //     center: ['50%', '50%'],
    //     roseType: 'radius',
    //     itemStyle: {
    //       borderRadius: 5
    //     },
    //     label: {
    //       show: false
    //     },
    //     emphasis: {
    //       label: {
    //         show: true
    //       }
    //     },
    //     data: [
    //       { value: 40, name: 'rose 1' },
    //       { value: 33, name: 'rose 2' },
    //       { value: 28, name: 'rose 3' },
    //       { value: 22, name: 'rose 4' },
    //       { value: 20, name: 'rose 5' },
    //       { value: 15, name: 'rose 6' },
    //       { value: 12, name: 'rose 7' },
    //       { value: 10, name: 'rose 8' }
    //     ]
    //   },
    // ]
  }
  // 3. 通过实例setOptions(options)
  myChart.setOption(options)
}

watch(
  () => props.data,
  () => { renderChart() }
)
</script>

<template>
  <div>
    <div>【大区异常处理】</div>
    <!-- bg-[#38bdf8]" -->
    <div ref="targetV" class="w-full h-full"></div>
  </div>
</template>

<style scoped>
.read-the-docs {
  color: #888;
}
</style>
